package com.yc.vote.controller;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.mybatis.util.RequestParamUtil;
import com.yc.vote.bean.UserInfo;
import com.yc.vote.dao.impl.UserInfoServiceImpl;
import com.yc.vote.service.IUserInfoService;

@WebServlet("/user/*")
public class UserInfoController extends BaseCotroller {
	private static final long serialVersionUID = -7276093678246959586L;

	/**
	 *user/login
	 * @param request
	 * @param response
	 * @throws IOException
	 **/
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		UserInfo uf = RequestParamUtil.getParams(request,UserInfo.class);
		IUserInfoService userINfoService = new UserInfoServiceImpl();
		UserInfo user = userINfoService.login(uf);
		if(user == null) {
			this.send(response, 500,"失败");
		}
		request.getSession().setAttribute("currentLoginUser", user);	
		this.send(response, 200,"成功");
		}
	/**
	 * 用户注册
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void reg(HttpServletRequest request, HttpServletResponse response) throws IOException {
		UserInfo uf = RequestParamUtil.getParams(request,UserInfo.class);
		IUserInfoService userINfoService = new UserInfoServiceImpl();
		int result = userINfoService.reg(uf);
		if(result > 0) {
			this.send(response, 200,"成功");
		}
		this.send(response, 500,"失败");
	}
	
	public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object obj = request.getSession().getAttribute("currentLoginUser");
		if(obj == null) {
			this.send(response, 600,"未登录");
			return;
		}
		
		this.send(response, 200,obj);
	}
}


	